package com.peanut.baby.mvp.profile.comments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.peanut.baby.R;
import com.peanut.baby.model.BBSComment;
import com.peanut.baby.mvp.BaseMvpFragment;
import com.peanut.baby.mvp.bbs.detail.BBSCommentRecyclerAdapter;
import com.peanut.baby.mvp.bbs.detail.BBSMomentDetailActivity;
import com.peanut.baby.mvp.qadetail.QADetailActivity;
import com.peanut.devlibrary.widget.pullrecycle.BaseRecyclerAdapter;
import com.peanut.devlibrary.widget.pullrecycle.PullRecyclerView;
import com.peanut.devlibrary.widget.pullrecycle.layoutmanager.XLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by anonymouslystudio on 2017/8/2.
 */

public class ProfileReplysFragment extends BaseMvpFragment implements PullRecyclerView.OnRecyclerRefreshListener,
    ProfileCommentsContract.View,BaseRecyclerAdapter.OnRecyclerItemClickListener{
    @BindView(R.id.pull_recycler)
    PullRecyclerView pullRecycler;
    Unbinder unbinder;

    private String userId = "";
    private List<BBSComment> comments;
    private BBSCommentRecyclerAdapter adapter;

    private ProfileCommentsPresenter presenter;

    private int pageNo = 0;
    private int pageSize = 10;

    public static ProfileReplysFragment newInstance(String userId){
        ProfileReplysFragment fg = new ProfileReplysFragment();
        Bundle bundle = new Bundle();
        bundle.putString("userId",userId);
        fg.setArguments(bundle);
        return fg;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_profile_moments, null);
        unbinder = ButterKnife.bind(this, rootView);
        pullRecycler.enablePullRefresh(false);
        pullRecycler.enableLoadMore(true);
        pullRecycler.setOnRecyclerRefreshListener(this);
        return rootView;
    }

    @Override
    public void initData() {
        userId = getArguments().getString("userId");
        presenter = new ProfileCommentsPresenter(this,getActivity());
        comments = new ArrayList<>();
        adapter = new BBSCommentRecyclerAdapter(getActivity(),comments);
        pullRecycler.setLayoutManager(new XLinearLayoutManager(getActivity()));
        pullRecycler.setAdapter(adapter);
        adapter.setOnRecyclerItemClickListener(this);

        presenter.getComments(userId,pageNo,pageSize);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        pageNo++;
        presenter.getComments(userId,pageNo,pageSize);
    }

    @Override
    public void onGetCommentsSuccess(List<BBSComment> _comments) {
        if(getActivity() == null){
            return;
        }
        pullRecycler.stopLoadMore();
        if(pageNo == 1){
            comments.clear();
        }
        if(_comments != null && !_comments.isEmpty()){
            comments.addAll(_comments);
        }
        adapter.notifyDataSetChanged();
        pullRecycler.enableLoadMore(_comments != null && _comments.size() >= pageSize);
    }

    @Override
    public void onGetCommentsFailed(String reason) {
        if(getActivity() != null){
            pullRecycler.stopLoadMore();
            pullRecycler.enableLoadMore(false);
            showToast(reason);
        }

    }

    @Override
    public void onItemClick(View view, int position) {
        int type = comments.get(position).type;
        if(type == 1){
            QADetailActivity.start(getActivity(),comments.get(position).objectId);
        }else{
            BBSMomentDetailActivity.start(getActivity(),comments.get(position).objectId+"");
        }

    }
}
